package iabincatalog.parser;

import iabincatalog.parse.exception.CsvFileCantReadException;
import iabincatalog.parse.exception.CsvFileNameNullException;
import iabincatalog.parse.exception.XmlFileCantReadException;
import iabincatalog.parse.exception.XmlFileNameNullException;

import java.io.File;

import org.apache.log4j.Logger;


/**
 * Utility class for parsing a XML file by using a CSV (Comma-Separated Value)
 * mapping.<br>
 * <br>
 * The CSV mapping should establish a relationship between each possible value
 * that could be found at the input XML and an valid output. If not defined, the
 * same value should be left unmodified.<br>
 */
public class XmlParser 
{
	// log4j Logger
	Logger logger = Logger.getLogger(XmlParser.class);
	
	/**
	 * Parses a XML file identified by theXmlFile using the CSV mapping 
	 * identified by theCsvFile.
	 * 
	 * @param theXmlfileName XML fileName
	 * @param theCsvfileName CSV mapping fileName 
	 * 
	 * @throws Exception If any error occurs.
	 */
	public void parseXML(String theXmlFileName, String theCsvFileName) throws Exception
	{
		logger.debug("Checking fileNames");
		if (theXmlFileName == null)
		{
			throw new XmlFileNameNullException();
		}
		if (theCsvFileName == null)
		{
			throw new CsvFileNameNullException();
		}
		
		logger.info("Parsing file: " + theXmlFileName);
		logger.info("Using map: " + theCsvFileName);
		
		logger.debug("Creating and checking File object for XML file");
		File myXmlFile = new File(theXmlFileName);
		if (!myXmlFile.canRead())
		{
			throw new XmlFileCantReadException(theXmlFileName);
		}
		
		logger.debug("Creating and checking File object for CSV file");
		File myCsvFile = new File(theCsvFileName);
		if (!myCsvFile.canRead())
		{
			throw new CsvFileCantReadException(theCsvFileName);
		}		
	}	
}